﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Collections;
using System.Reflection;
using UMC.Wiki.Entities;
using System.IO;
using UMC.Web;
using UMC.Web.UI;

namespace UMC.Wiki.Activities
{



    class SubjectFollowActivity : WebActivity
    {

        public override void ProcessActivity(WebRequest request, WebResponse response)
        {


            var user = this.Context.Token.Identity();
            var sId = this.AsyncDialog("Id", g =>
            {
                var form = request.SendValues ?? new UMC.Web.WebMeta();


                int limit = UMC.Data.Utility.IntParse(form["limit"] as string, 25);
                int start = UMC.Data.Utility.IntParse(form["start"] as string, 0);



                var Keyword = form["Keyword"] as string ?? String.Empty;


                var ui = UISection.Create();
                int nextIndex;
                var subs = DataFactory.Instance().Follow(new Subject { user_id = user.Id, Title = Keyword }, start, limit, out nextIndex);

                SubjectUIActivity.Search(ui, subs, request.Model, "UIData", false, request.IsApp, request.Url);


                ui.StartIndex = nextIndex;
                ui.IsNext = nextIndex > 0;

                if (start == 0 && ui.Length == 0)
                {
                    ui.Add("Desc", new UMC.Web.WebMeta().Put("desc", "未有您的关注的项目").Put("icon", "\uF016"), new UMC.Web.WebMeta().Put("desc", "{icon}\n{desc}"),

                        new UIStyle().Align(1).Color(0xaaa).Padding(20, 20).BgColor(0xfff).Size(12).Name("icon", new UIStyle().Font("wdk").Size(60)));
                }
                response.Redirect(ui);
                return this.DialogValue("none");
            });

            var UId = UMC.Data.Utility.UInt64Parse(sId);
            var project = DataFactory.Instance().Project(UId.Value);
            if (user.IsAuthenticated == false)
            { 
                this.Context.Send("Login", true);
            }
            var projectMember = DataFactory.Instance().ProjectMember(project.Id.Value, user.Id.Value);
            if (projectMember == null)
            {

                DataFactory.Instance().Put(new ProjectMember
                {
                    CreationTime = DateTime.Now,
                    AuthType = Web.WebAuthType.Guest,
                    project_id = project.Id,
                    user_id = user.Id,
                    Alias = user.Alias
                });

                DataFactory.Instance().Put(new ProjectDynamic
                {
                    Time = UMC.Data.Utility.TimeSpan(DateTime.Now),
                    user_id = user.Id,
                    Explain = "关注了项目",
                    project_id = project.Id,
                    refer_id = 0,//user.Id,
                    Alias = user.Alias,
                    Title = "项目成员",
                    Type = DynamicType.Member
                });

                var ax = DataFactory.Instance().Attention(user.Id.Value) ?? new Attention { Id = user.Id };
                DataFactory.Instance().Put(new Attention
                {
                    Id = user.Id,
                    ProjectTotal = (ax.ProjectTotal ?? 0) + 1
                });
                DataFactory.Instance().Put(new Project
                {
                    Id = project.Id,
                    MemberTotal = (project.MemberTotal ?? 0) + 1
                });
            }
            else
            {
                switch (projectMember.AuthType ?? WebAuthType.Guest)
                {
                    case WebAuthType.Admin:
                        if (DataFactory.Instance().ProjectMember(project.Id.Value, WebAuthType.Admin).Length == 1)
                        {
                            this.Prompt("你是最后一个项目主，不能退出此项目");
                        }
                        else
                        {
                            this.AsyncDialog("Confirm", d => new UIConfirmDialog("你是此项目的项目主，确定退出此项目组吗") { Title = project.Caption });

                        }
                        if ((project.ExpireTime ?? 0) == 0)
                        {
                            this.Prompt("此为个人项目，不能改变项目主");
                        }
                        break;
                    case WebAuthType.User:
                        this.AsyncDialog("Confirm", d => new UIConfirmDialog("你是此项目的专栏作家，确定退出此项目组吗") { Title = project.Caption });
                        break;
                    default:
                        this.AsyncDialog("Confirm", d => new UIConfirmDialog("你确定退出此项目组吗") { Title = project.Caption });
                        break;
                }
                DataFactory.Instance().Delete(new ProjectMember { user_id = user.Id, project_id = project.Id.Value });
                var ax = DataFactory.Instance().Attention(user.Id.Value) ?? new Attention { Id = user.Id };
                DataFactory.Instance().Put(new Attention
                {
                    Id = user.Id,
                    ProjectTotal = (ax.ProjectTotal ?? 0) - 1
                });
                DataFactory.Instance().Put(new Project
                {
                    Id = project.Id,
                    MemberTotal = (project.MemberTotal ?? 0) - 1
                });
            }

            this.Context.Send("Subject.Member", true);
        }

    }
}